package com.crm.manager.service.impl;

import com.crm.common.core.domain.entity.SysUser;
import com.crm.manager.domain.dto.TransferPageDTO;
import com.crm.manager.domain.vo.PageVO;
import com.crm.manager.domain.vo.TransferVO;
import com.crm.manager.mapper.BusinessMapper;
import com.crm.manager.mapper.TransferMapper;
import com.crm.manager.service.TransferService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private TransferMapper transferMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageVO pageQuery(TransferPageDTO dto) {
        PageVO pageVO = new PageVO();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<TransferVO> list = (Page<TransferVO>) transferMapper.pageQuery(dto, dto.getParams().get("beginTime").toString(), dto.getParams().get("endTime").toString());

        pageVO.setTotal(list.getTotal());
        pageVO.setRows(list.getResult());
        return pageVO;
    }

    /**
     * 转派，把fromUserId的商机或者线索（type=0|1）给到toUserId
     * //1：0代表线索，1代表商机 2，原来的归宿人 3，转派给谁
     * @param type
     * @param fromUserId
     * @param toUserId
     */
    @Override
    public void assignment(Integer type, Integer fromUserId, Integer toUserId) {
        //根据userId查到name
        SysUser targetUser = businessMapper.queryUserById(toUserId);
        //先判断是线索还是商机
        //是商机
        if (type.equals(1)){
            transferMapper.assignment(fromUserId, toUserId, targetUser.getUserName());
        }
        //TODO 是线索
        if (type.equals(0)){
            //根据id查名字,这是查原来的归属人
            SysUser fromUser = businessMapper.queryUserById(fromUserId);
            transferMapper.assignmentClue(fromUser.getUserName(), targetUser.getUserName());
        }
    }
}
